<template>
    <div>
        <el-form
                class="loginContainer"
                :model="user"
                v-loading.fullscreen.lock="loading"
                element-loading-text="正在登录..."
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                :rules="rules"
                ref="loginForm">
            <h3 class="loginTitle">系统登录</h3>
            <el-form-item prop="username">
                <el-input v-model="user.username" size="small" type="text" placeholder="请输入用户名"/>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="user.password" size="small" type="password" placeholder="请输入密码"/>
            </el-form-item>
            <el-form-item prop="code">
                <el-input size="small" type="text" v-model="user.code" auto-complete="off"
                          placeholder="点击图片更换验证码" @keydown.enter.native="login" style="width: 250px;vertical-align: middle"></el-input>
                <img :src="vcUrl" @click="updateVerifyCode" alt="" style="cursor: pointer;vertical-align: middle">
            </el-form-item>
            <el-button @click="login" type="primary" style="width: 100%;">登录</el-button>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data: function () {
            return {
                user: {
                    username: 'admin',
                    password: '123',
                    code: ''
                },
                vcUrl: 'http://localhost:8081/vhr/hr/verifyCode',
                loading: false,
                rules: {
                    username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
                    password: [{required: true, message: '请输入密码', trigger: 'blur'}]
                }
            }
        },
        methods: {
            login() {
                this.$refs.loginForm.validate((valid) => {
                    if (valid){
                        // 使用axios发送请求
                        this.$request('/login','post',this.user)
                        .then(res => {
                            if (res){
                                // 将登录成功的用户放入sessionstorage中
                                sessionStorage.setItem("current_hr",JSON.stringify(res.obj))
                                // 修改状态仓库中的hr
                                this.$store.commit('chage_hr',res.obj)
                                // 切换主组件
                                this.$router.push("/welcome")
                            }
                        })
                    }else{
                        this.$message.error('请输入用户名和密码！')
                    }
                })
            },
            updateVerifyCode(){
                this.vcUrl = 'http://localhost:8081/vhr/hr/verifyCode?time='+new Date()
            }
        }
    }
</script>

<style scoped>
    .loginContainer {
        border-radius: 15px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 15px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }

    .loginTitle {
        margin: 15px auto 20px auto;
        text-align: center;
        color: #505458;
    }

    .loginRemember {
        text-align: left;
        margin: 0px 0px 15px 0px;
    }

    .el-form-item__content {
        display: flex;
        align-items: center;
    }
</style>